<?

if (!$user)
{
  include "page_personal_logon.php";
  return;
}

//!!!
//$PageTitle=$msg["Page_ChangeProfile"];
$PageTitle="Personal settings";

include "div_menu_personal.php";


if (!$_POST) 
{
  make_form($user);
}
else
{
  $new_user = array(
    "id"=>$user["id"],
    "theme"=>trim(stripslashes($_POST["theme"]))
  );

  if (save_settings($new_user, $errors)) 
  {
    $_SESSION["user"] = db_read_user_by_id($user["id"]);

    print_continue($msg["Saved"], "personal");
  } 
  else 
  {
    print_errors($errors);
    make_form($new_user);
  };
};


//-----------------------------------------------------------------------------

function make_form($user) 
{
  global $msg, $user;

  $themes = array();
  $folders = scandir("themes");
  foreach ($folders as $folder)
  {
    if (($folder <> ".") && ($folder <> ".."))
    {
      $filename = "themes\\".$folder."\\default.css";
      if (file_exists($filename))
      {
        array_push($themes, $folder);
      }
    }
  }

?>

<!-- Settings form -->

<div class="form_area">
<div class="form_div">

<form method="post">

  <fieldset class="fields1">

  <dl>
    <dt><label for="name">Theme:</label></dt>
<!--<dd><input type="text" size="25" tabindex="1" name="name" id="name" value="<?=maskqoute($user["theme"]);?>"/></dd>-->

    <dd>
    <select tabindex="5" name="theme" id="theme" style="width:150px">
      <option value="">default</option>
<?
      foreach ($themes as $theme)
      {
        $selected = ($theme == $user["theme"]) ? "selected" : "";
        print "<option value=\"{$theme}\" $selected>{$theme}</option>\n";
      };
?>
    </select>
    </dd>

  </dl>

  <dl class="button_div">
    <button type="submit" tabindex="5" name="save"><?=$msg["Save"];?></button>
    &nbsp;
    <button type="button" tabindex="6" name="cancel" onClick="window.location.href='personal'"><?=$msg["Cancel"];?></button>
  </dl>

  </fieldset>

</form>

</div>
</div>

<?php
};


function save_settings($user, &$error) 
{
  global $msg;

  $n = 0;

  try 
  {
    db_call();

    if ($n == 0) 
    {
      db_update_settings($user["id"], $user);
    }
  } 
  catch (Exception $e) 
  {
    $errors[$n++] = $e->getMessage();
  }

  return $n == 0;
};

?>
